

import java.math.BigDecimal;
import java.math.MathContext;


/**
 * 
 * @author Nizar
 * 
 */

public class DivideBinExpr extends BinExpr {

	public DivideBinExpr(Expr leftExpr, Expr rightExpr) {
		super(leftExpr, rightExpr);

	}
	public BigDecimal evaluate(Env env) {

		if(rightExpr.evaluate().equals(BigDecimal.ZERO)){

			throw new RuntimeException("Divisione per zero"); 
		}

		BigDecimal value = leftExpr.evaluate(env).divide(rightExpr.evaluate(env), MathContext.DECIMAL128);

		return value;
	}
	
	public static DivideBinExpr _(Expr leftExpr, Expr rightExpr){
		return new DivideBinExpr(leftExpr, rightExpr);
	}
	@Override
	public String toString() {
		String leftExprTmp = leftExpr.toString();
		String rightExprTmp = "("+rightExpr.toString()+")";
		
		if(leftExpr.isPlusBinExpr() || leftExpr.isMinusBinExpr())
			leftExprTmp = "("+leftExprTmp+")";
		
		if(rightExpr.isUnExpr() || rightExpr.isNum() || rightExpr.isId())
			rightExprTmp = rightExpr.toString();
		
		return leftExprTmp+" / "+rightExprTmp;
	}
	@Override
	public boolean isUnExpr() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isAssignExpr() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isPlusBinExpr() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isMinusBinExpr() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isTimesBinExpr() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isDivideBinExpr() {
		// TODO Auto-generated method stub
		return true;
	}
	@Override
	public boolean isNum() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isId() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean isSeqExpr() {
		// TODO Auto-generated method stub
		return false;
	}
}
